package com.itheima;

import java.util.Random;

/**
 * 2025/3/29 10:25
 * 生成验证码
 */
public class LoopPractice3 {
    public static void main(String[] args) {
        String code = getCode(5);
        System.out.println("验证码是：" + code);
    }
    public static String getCode(int n) {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < n; i++) {
            //判断当次循环要生成数字 大写字母 小写字母
            int type = random.nextInt(3);//[0,2]
            switch (type) {
                case 0://数字
                    int number = random.nextInt(10);
                    //字符串的拼接
                    code += number;
                    break;
                case 1://大写字母
                    //先生成一个数字【65,90】
                    int charNumber = random.nextInt(65, 91);
                    //将数字转字符
                    char c = (char) charNumber;
                    //将字符拼接到字符串上
                    code += c;
                    break;
                case 2://小写字母
                    //先生成一个数字【97,122】
                    int lowCharNumber = random.nextInt(97, 122);
                    //将数字转字符
                    char lc = (char) lowCharNumber;
                    //将字符拼接到字符串上
                    code += lc;
                    break;
            }
        }
        return code;
    }

}
